// pages/mine/info/mob.js
const App = getApp()
import Util from '../../../utils/util'

Page({
  data: {
    init: false,

    butText: '验证码',
    butDisabled: false,

    mob: '',
    vinfo: '',
    vcode: '',

    interval: undefined,
    sec: 0,


  },
  onLoad() {
    this.setData({
      init: true
    })
  },
  onUnload() {
    if (this.data.interval != undefined) {
      clearInterval(this.data.interval)
    }
  },
  execGetVcode() {
    if (this.data.mob.length != 11) {
      return
    }
    if (this.data.butDisabled) {
      return
    }
    this.setData({
      loading: true,
      butDisabled: true
    })
    App.doPost({
      uri: '/app/base/tool/sms/captcha',
      body: {
        mob: this.data.mob
      },
      fnSucc: resp => {
        this.setData({
          sec: resp.data.limit,
          vinfo: resp.data.vinfo,
        })
        let int = setInterval(() => {
          if (this.data.sec <= 1) {
            clearInterval(this.data.interval)
            this.setData({
              sec: 0,
              butText: '验证码',
              butDisabled: false,
              interval: undefined
            })
            return
          }
          this.data.sec = this.data.sec - 1
          this.setData({
            sec: this.data.sec,
            butText: `${this.data.sec} 秒`
          })
        }, 1000);
        this.setData({
          interval: int
        })
        if (App.Global.Debug && resp.data.vcode) {
          wx.showToast({
            icon: 'none',
            title: '测试账号，验证码已自动填写'
          })
          this.setData({
            vcode: resp.data.vcode
          })
        } else {
          wx.showToast({
            title: '验证码已发送'
          })
        }

      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
        this.setData({
          butDisabled: false,
        })
      },
      fnComp: () => {
        this.setData({
          loading: false,
        })
      }
    })
  },
  execSave() {
    if (!this.data.mob) {
      Util.Notify.warn(`请填写手机号`)
      return
    }
    if (!this.data.vinfo) {
      Util.Notify.warn(`请获取验证码`)
      return
    }
    if (!this.data.vcode) {
      Util.Notify.warn(`请填写验证码`)
      return
    }

    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/user/info/profile/mob',
      body: {
        mob: this.data.mob,
        vinfo: this.data.vinfo,
        vcode: this.data.vcode
      },
      fnSucc: () => {
        wx.pub("ReloadUserInfo", {})
        Util.Toast.succ(`提交成功`)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      },
      fnFail: resp => {
        clearInterval(this.data.interval)
        this.setData({
          sec: 0,
          vcode: '',
          vinfo: ''
        })
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false,
        })
      }
    })
  }
})